package sg.edu.nus.iss.stockex.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;

import sg.edu.nus.iss.JOB;
import sg.edu.nus.iss.stockex.MovingAverage;
import sg.edu.nus.iss.stockex.StockCode;
import sg.edu.nus.iss.stockex.StockQuote;

/**
 * Servlet implementation class StockQuoteServlet
 */
public class StockQuoteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AmazonEC2      ec2;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StockQuoteServlet() {
        super();
        if (ec2 == null) {
            AWSCredentials credentials = null;
			try {
				credentials = new PropertiesCredentials(
				    getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties"));
			} catch (IOException e) {
				e.printStackTrace();
			}
            ec2 = new AmazonEC2Client(credentials);
            ec2.describeInstances().getReservations();
        }
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JOB job = new JOB();
		List<StockCode> stockCodeList = job.getAllStockCodes();
		for (StockCode code : stockCodeList)
		System.out.println("code = "+ code.getCode());
		request.getSession().setAttribute("stockCodeList", stockCodeList);
		getServletContext().getRequestDispatcher("/quote.jsp").forward(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("### do post");
		JOB job = new JOB();		
		String stockCode = request.getParameter("code");
		int quarter = Integer.parseInt(request.getParameter("quarter"));
		int year = Integer.parseInt(request.getParameter("year"));
		
		String startDate = "";
		String endDate = "";
		
		if (quarter == 1){
			startDate = year +"-05-01";
			endDate = year +"-07-31";
		} else if (quarter == 2){
			startDate = year +"-08-01";
			endDate = year +"-10-31";
		} else if (quarter == 3){
			startDate = year +"-11-01";
			endDate = (year+1) +"-01-31";
		} else if (quarter == 4){
			startDate = (year+1) +"-02-01";
			endDate = (year+1) +"-04-30";
		}
		
		List<StockQuote> stockList = job.getStock(stockCode, startDate, endDate);
		System.out.println("stockList.size() = " + stockList.size());
		
		request.getSession().setAttribute("stockList", stockList);
		
		getServletContext().getRequestDispatcher("/quote.jsp").forward(request, response);
	
	}

}
